package com.bbm.communitysystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.domain.Household;
import com.bbm.communitysystem.domain.bo.HouseholdBo;
import com.bbm.communitysystem.domain.dto.HouseholdDto;
import com.bbm.communitysystem.mapper.HouseholdMapper;
import com.bbm.communitysystem.service.IHouseholdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bbm
 * @since 2025-02-21
 */
@Service
public class HouseholdServiceImpl extends ServiceImpl<HouseholdMapper, Household> implements IHouseholdService {

    @Override
    public int createHousehold(HouseholdBo householdBo) {

        Household household = BeanUtil.copyProperties(householdBo, Household.class);
        household.setUpdateTime(System.currentTimeMillis() + "");
        boolean save = save(household);

        return save ? 1 : 0;
    }

    @Override
    public int updateHousehold(HouseholdDto dto) {
        boolean success = lambdaUpdate()
                .set(dto.getHouseholdName() != null && !dto.getHouseholdName().isEmpty(), Household::getHouseholdName, dto.getHouseholdName())
                .set(dto.getPhone() != null && !dto.getPhone().isEmpty(), Household::getPhone, dto.getPhone())
                .set(dto.getCardNum() != null && !dto.getCardNum().isEmpty(), Household::getCardNum, dto.getCardNum())
                .set(dto.getSex() != null , Household::getSex, dto.getSex())
                .set(dto.getCurReside() != null && !dto.getCurReside().isEmpty(), Household::getCurReside, dto.getCurReside())
                .set(dto.getBirthDate() != null && !dto.getBirthDate().isEmpty(), Household::getBirthDate, dto.getBirthDate())
                .set(dto.getEthnicGroup() != null && !dto.getEthnicGroup().isEmpty(), Household::getEthnicGroup, dto.getEthnicGroup())
                .set(dto.getHouseholdRmake() != null && !dto.getHouseholdRmake().isEmpty(), Household::getHouseholdRmake, dto.getHouseholdRmake())
                .set(dto.getCommunilyId() != null && dto.getCommunilyId() != 0, Household::getCommunilyId, dto.getCommunilyId())

                .set(Household::getUpdateTime, System.currentTimeMillis() + "")
                .set(dto.getUpdateBy() != null, Household::getUpdateBy, dto.getUpdateBy())
                .eq(Household::getId, dto.getId())
                .update();
        return success ? 1 : 0;
    }


}
